package com.wolf.cx330.view.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View


class SportTextView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {

    private val rectF = RectF()
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val radius = 300F
    private val drawText = "abab"
    private val textBound = Rect()
    val fontMetrics = Paint.FontMetrics()
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        rectF.set(
            width / 2F - radius,
            height / 2F - radius,
            width / 2F + radius,
            height / 2F + radius
        )
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        paint.apply {
            color = Color.RED
            style = Paint.Style.STROKE
            strokeWidth = 20F
        }
        canvas.drawArc(rectF, 0F, 360F, false, paint)
        paint.apply {
            color = Color.BLUE
            strokeCap = Paint.Cap.ROUND
        }
        canvas.drawArc(rectF, -105F, 270F, false, paint)
        //  画字
        paint.apply {
            textAlign = Paint.Align.CENTER
            color = Color.BLACK
            style = Paint.Style.FILL
            textSize = 200F
        }
        paint.getTextBounds(drawText, 0, drawText.length, textBound)
        paint.getFontMetrics(fontMetrics)
        //  上下居中显示的两种做法
        val offset = (textBound.top + textBound.bottom) / 2F
//        val offset = (fontMetrics.ascent + fontMetrics.descent) / 2F
        canvas.drawText(
            drawText,
            width / 2F,
            height / 2F - offset,
            paint
        )

    }

}